class Solution:
    #只能买一次卖一次
    def maxProfit(self, prices):
        maxProfit = 0
        minPrice = float('inf')
        for price in prices:
            minPrice = min(minPrice,price)
            maxProfit = max(maxProfit,price - minPrice)

        return maxProfit

class Solution2:
    def maxProfit(self, prices):
        flag = False
        profit = 0
        for i in range(len(prices)-1):
            if prices[i] < prices[i+1] and not flag:
                profit -= prices[i]
                flag = not flag
            if prices[i] > prices[i+1] and flag:
                profit += prices[i]
                flag = not flag
        if flag:
            profit += prices[-1]
        return profit
